package com.hc.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hc.entity.House;
import com.hc.entity.Logistics;
import com.hc.entity.Repair;
import com.hc.service.RepairService;
import com.hc.serviceimp.RepairServiceImpl;


@WebServlet("/RepairServiceServlet")
public class RepairServiceServlet extends HttpServlet {
	RepairService rService=new RepairServiceImpl();
    public RepairServiceServlet() {
        super();
        
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		if (action.equals("toadd")) {
			List<House> houses = rService.getallhouse();
			List<Logistics> logistics = rService.getalllogistics();
			
			request.setAttribute("house", houses);
			request.setAttribute("logis", logistics);
			request.getRequestDispatcher("/WEB-INF/repair/add.jsp").forward(request, response);
		}
		if (action.equals("add")) {
			String hid = request.getParameter("hid");
			String lid = request.getParameter("lid");
			String rpmessage = request.getParameter("rpmessage");
			String rpdate = request.getParameter("rpdate");
			String rpnote = request.getParameter("rpnote");
			String reresult = request.getParameter("rpresult");
			Repair repair = new Repair(0, rpmessage, rpdate, reresult, rpnote, 
					new House(Integer.parseInt(hid), null, null, 0, 0, 0, 0, null, null, null, 0, null, 0, null, null, null, null), 
					new Logistics(Integer.parseInt(lid), null, null, 0, null, null, null, 0, null));
			
			rService.add(repair);
			
			request.getRequestDispatcher("RepairServiceServlet?action=tolist").forward(request, response);
		}
		
		if (action.equals("tolist")) {
			
			List<House> houses = rService.getallhouse();
			
			request.setAttribute("house", houses);
			request.getRequestDispatcher("/WEB-INF/repair/select.jsp").forward(request, response);
		}
		
		
		if (action.equals("list")) {
			String id = request.getParameter("hid");
			String date = request.getParameter("rpdate");
			List<Repair> repairs = rService.select(Integer.parseInt(id), date);
			
			request.setAttribute("repair", repairs);
			request.getRequestDispatcher("/WEB-INF/repair/list.jsp").forward(request, response);
		}
		
		
		if (action.equals("toupdate")) {
			String id = request.getParameter("id");
			List<House> houses = rService.getallhouse();
			List<Logistics> logistics = rService.getalllogistics();
			Repair repair = rService.show(Integer.parseInt(id));
			
			
			request.setAttribute("r", repair);
			request.setAttribute("logistic", logistics);
			request.setAttribute("house", houses);
			request.getRequestDispatcher("/WEB-INF/repair/edit.jsp").forward(request, response);
		}
		
		if (action.equals("update")) {
			String rpid = request.getParameter("rpid");
			String hid = request.getParameter("hid");
			String lid =request.getParameter("lid");
			String rpmessage =request.getParameter("rpmessage");
			String rpdate =request.getParameter("rpdate");
			String reresult =request.getParameter("reresult");
			String rpnote =request.getParameter("rpnote");
			Repair repair = new Repair(Integer.parseInt(rpid), rpmessage, rpdate, reresult, rpnote, new House(Integer.parseInt(hid), null, null, 0, 0, 0, 0, null, null, null, 0, null, 0, null, null, null, null), 
					new Logistics(Integer.parseInt(lid), null, null, 0, null, null, null, 0, null));
			
			rService.update(repair);
			System.out.println("更新成功");
			request.getRequestDispatcher("RepairServiceServlet?action=tolist").forward(request, response);
			
			
		}
		if (action.equals("del")) {
			
			String id = request.getParameter("id");
			rService.delete(Integer.parseInt(id));
			
			request.getRequestDispatcher("RepairServiceServlet?action=tolist").forward(request, response);
		}
		
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
